package com.cb.web.wasf;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;

/**
 * @author CBO
 */
public class Request {

    public static final String EVENT_RPN = "event";
    public static final String EVENT_SOURCE_RPN = "eventSource";
    public static final String ASYNC_RPN = "async";
    private DefaultSession session;
    private HttpServletRequest httpServletRequest;

    Request(DefaultSession session, HttpServletRequest httpServletRequest) {
        this.session = session;
        this.httpServletRequest = httpServletRequest;
    }

    public DefaultSession getSession() {
        return session;
    }

    public String getParameter(String name) {
        return httpServletRequest.getParameter(name);
    }

    public Set<String> getEmptyToNullParameterSet(String name) {
        String[] parameterValues = httpServletRequest.getParameterValues(name);
        if (parameterValues != null) {
            Set<String> toReturn = new HashSet<String>();
            toReturn.addAll(Arrays.asList(parameterValues));
            return toReturn;
        } else {
            return Collections.emptySet();
        }
    }

    public String getEmptyToNullParameter(String parameterName) {
        return StringUtils.emptyToNull(getParameter(parameterName));
    }

    public boolean isBlankParameter(String parameterName) {
        return StringUtils.isBlank(getParameter(parameterName));
    }

    public String getTrimmedParameter(String name) {
        return getParameter(name) != null ? getParameter(name).trim() : null;
    }

    public String getEvent() {
        return getEmptyToNullParameter(EVENT_RPN);
    }

    public String getEventSource() {
        return getEmptyToNullParameter(EVENT_SOURCE_RPN);
    }
}
